package com.prs.crm.service.oa.impl;

import java.io.File;
import java.util.Collection;
import java.util.Iterator;

import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.oa.NoticeDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Notice;
import com.prs.crm.domain.oa.NoticeAccept;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.oa.NoticeAcceptService;
import com.prs.crm.service.oa.NoticeService;

public class NoticeServiceImpl extends BaseServiceImpl<Notice,NoticeDao> implements NoticeService{
	private PersonService personService;
	private NoticeAcceptService noticeAcceptService;
	
	
	public 	void sendNotice(Notice notice,Person person)
	{
		this.getDao().save(notice);
		NoticeAccept a = new NoticeAccept();
//		a.setNotice(notice);
		a.setPerson(person);
		a.setState(Constrains.NOTICE_UNREAD);
		this.getNoticeAcceptService().save(a);
	}
	
	@Override
	public void sendNotice(String title, String msg, Person person) {
		// TODO Auto-generated method stub
		Notice notice = new Notice();
		notice.setTitle(title);
		notice.setContent(msg);
		this.getDao().save(notice);
		NoticeAccept a = new NoticeAccept();
//		a.setNotice(notice);
		a.setPerson(person);
		a.setState(Constrains.NOTICE_UNREAD);
		this.getNoticeAcceptService().save(a);
	}	
	
	@Override
	public void saveNotice(Notice notice, File file, String fileName,String accepts) {
		// TODO Auto-generated method stub
		notice.setAttach(FileUtil.getInstance().createAttach(file, fileName));
		this.getDao().save(notice);
		
		Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(accepts.split(",")));
		Iterator<Person> iter = acceptPersons.iterator();
		while(iter.hasNext()) {
			Person person = iter.next();
			NoticeAccept a = new NoticeAccept();
//			a.setNotice(notice);
			a.setPerson(person);
			a.setState(Constrains.NOTICE_UNREAD);
			
			this.getNoticeAcceptService().save(a);
		}		
	}
	@Override
	public void saveNoticeToCaogao(Notice notice, File file, String fileName) {
		// TODO Auto-generated method stub
		notice.setAttach(FileUtil.getInstance().createAttach(file, fileName));
		this.getDao().save(notice);
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public NoticeAcceptService getNoticeAcceptService() {
		return noticeAcceptService;
	}

	public void setNoticeAcceptService(NoticeAcceptService noticeAcceptService) {
		this.noticeAcceptService = noticeAcceptService;
	}

	@Override
	public Collection<Notice> getAllNotice() {
		// TODO Auto-generated method stub
		return this.getDao().getAllNotice();
	}

	@Override
	public Collection<Notice> getNoticeByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		return this.getDao().getNoticeByIds(ToolKits.convertToIntAry(ids));
	}

	@Override
	public Collection<Notice> getUnreadNotice(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getUnreadNotice(person);
	}

	@Override
	public Notice getNoticeId(Integer id) {
		return this.getDao().getNoticeId(id);
	}

}
